<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>promise</title>
</head>
<body>
  
  <script>
    // function execCode(counter) {
    //   const promise = new Promise((resolve, reject) => {
    //     //异步任务
    //     setTimeout(() => {
    //       if (counter > 0) {
    //         let total = 0
    //         for (let i = 0; i < counter; i++) {
    //           total += 1              
    //         }
    //         resolve(total)
    //       } else {
    //         reject(`${counter}有问题`)
    //       }
    //     }, 3000);
    //   })
    //   return promise
    // }
    const execCode = (counter) => {
      const promise = new Promise((resolve, reject) => {
        //异步任务
        setTimeout(() => {
          if (counter > 0) {
            let total = 0
            for (let i = 0; i < counter; i++) {
              total += 1              
            }
            resolve(total)
          } else {
            reject(`${counter}有问题`)
          }
        }, 3000);
      })
      return promise
    }
    execCode(255).then(value => {
      console.log("成功：", value)
    }).catch(err => {
      console.log("失败：", err)
    })
  </script>
</body>
</html>